<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/6/29
 * Time: 16:28
 */

namespace app\api\controller\worker;

use app\api\controller\Base;
use app\common\model\AdminModel;
use app\common\model\node\NodeProcessModel;
use app\common\model\sale\SaleAfterModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;

class SaleAfterController extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new SaleAfterModel();
        $this->taskModel = new TaskModel();

    }

    //申请售后
    public function add(){

        $param = $this->request->param();
        $rule = [
            'customer_id|客户id'=>'require',
            'sale_name|售后联系人'=>'require',
            'sale_moblie|电话'=>'require',
            'is_make|是否约好上门时间'=>'require',
            'is_pay|是否向客户收费'=>'require',
            'sale_imgs|售后内容'=>'require',
            'sale_admin_id|售后内容'=>'require',
            'is_pay_wage|上传CAD图纸'=>'require',
        ];
        if($param['is_pay'] == 1){
            $rule['amount|向客户收费'] = 'require';
        }
        if($param['is_pay_wage'] == 1){
            $rule['wage_amount|向客户收费'] = 'require';
        }

        $this->validate($param,$rule);
        $data = [
            'customer_id'=>$param['customer_id'],
            'sale_name'=>$param['sale_name'],
            'sale_moblie'=>$param['sale_moblie'],
            'is_make'=>$param['is_make'],
            'is_pay'=>$param['is_pay'],
            'sale_admin_id'=>$param['sale_admin_id'],
            'is_pay_wage'=>$param['is_pay_wage'],
            'amount'=>$param['amount'] ?? '',
            'wage_amount'=>$param['wage_amount'] ?? '',
            'make_time'=>$param['make_time'] ?? '',
            'pay_remark'=>$param['pay_remark'] ?? '',
            'remark'=>$param['remark'] ?? '',
            'action_admin_id'=> $this->loginInfo['id'],
            'sale_imgs' => json_encode($param['sale_imgs'],JSON_UNESCAPED_UNICODE),
        ];
        $data['action_admin_id'] = $this->loginInfo['id'];
        // 启动事务
        Db::startTrans();
        $data['sale_time'] = date('Y-m-d H:i:s');
        $info = $this->model->add($data);
        if(!$info){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        $nodeProcess = NodeProcessModel::where('node_id', 14)->order('id', 'asc')->find();
        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 14,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $nodeProcess['audit_admin_id'],
            'action_table_id'=>$info['id'],//操作表id
             'admin_id'=>$this->loginInfo['id']
        ];
        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);
        $this->result($info,'操作成功',1);
    }


}